package com.ftg.youx.demo.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者
 *
 * @author 布衣
 */
public class Product {

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");
        try(
            Connection connection =  factory.newConnection();
            //创建频道
            Channel channel = connection.createChannel();
        ) {
            String queueName = "queue01";
            //创建队列
            channel.queueDeclare(queueName,false,false,false,null);

            for (int i = 0; i < 20000; i++) {
                //发送信息
                String message = "你好!世界"+i;
                channel.basicPublish("",queueName,null,message.getBytes());
                System.out.println("我发送完成了");
            }

        } catch (IOException | TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
}
